package NGuessNumberGUI;

import Common.Game;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.JTextPane;
import java.awt.Color;

public class Main extends JFrame implements Game {
	
	int[] guesses = {-1, -1, -1, -1};
	static int[] answers = {-1, -1, -1, -1};
	JTextField g1;
	JTextField g2;
	JTextField g3;
	JTextField g4;
	JLabel hint;
	JButton playAgain;
	JButton guess;
	
	public static void generateAnswer() {
		while(answers[3] == -1) {
			int candidate = (int)(Math.random() * 10);
			for(int i = 0; i < 4; i++) {
				if(answers[i] == -1) {
					answers[i] = candidate;
					break;
				}else if(answers[i] == candidate) {
					break;
				}
			}
		}
	}
	
	public void getGuess() {
		try {
			guesses[0] = Integer.parseInt(g1.getText());
			guesses[1] = Integer.parseInt(g2.getText());
			guesses[2] = Integer.parseInt(g3.getText());
			guesses[3] = Integer.parseInt(g4.getText());
		}catch(NumberFormatException e) {
			hint.setText("请输入数字！");
		}
		
	}
	
	public int getHint() {
		int acount = 0;
		int bcount = 0;
		for(int i = 0;i < 4; i++) {
			if(answers[i] == guesses[i]) {
				acount += 1;
			}
		}
		
		for(int i = 0; i < 4; i++) {
			for(int j = 0; j < 4; j++) {
				if(i != j && answers[i] == guesses[j]) {
					bcount += 1;
					break;
				}
			}
		}
		hint.setText(acount+" A "+bcount+" B");
		return acount;
	}
	
	public void initGame() {
		for(int i = 0; i < 4; i++) {
			guesses[i] = -1;
			answers[i] = -1;
		}
		generateAnswer();
		hint.setText("0 A 0 B");
		playAgain.setEnabled(false);
		g1.setText("");
		g2.setText("");
		g3.setText("");
		g4.setText("");
	}
	
	public void printData(int[] data) {
		for(int i = 0;i < 4; i++) {
			System.out.print(" " + data[i]);
		}
		System.out.println("\n");
	}

	public void run(){
		setSize(400, 300);
		setTitle("新猜数字");
		setLocation(450, 200);
		getContentPane().setLayout(null);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		JLabel title = new JLabel("新猜数字");
		title.setFont(new Font("宋体", Font.BOLD, 16));
		title.setBounds(155, 10, 73, 38);
		getContentPane().add(title);

		g1 = new JTextField();
		g2 = new JTextField();
		g3 = new JTextField();
		g4 = new JTextField();

		g1.setBounds(62, 93, 30, 30);
		g2.setBounds(134, 93, 30, 30);
		g3.setBounds(216, 93, 30, 30);
		g4.setBounds(298, 93, 30, 30);

		getContentPane().add(g1);
		getContentPane().add(g2);
		getContentPane().add(g3);
		getContentPane().add(g4);

		JLabel method = new JLabel("\u8BF7\u8F93\u51654\u4E2A0-9\u4E4B\u95F4\u7684\u6570\u5B57");
		method.setFont(new Font("宋体", Font.PLAIN, 14));
		method.setBounds(109, 58, 165, 15);
		getContentPane().add(method);

		guess = new JButton("\u731C\u4E00\u4E0B");
		guess.setFont(new Font("宋体", Font.PLAIN, 13));
		guess.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getGuess();
				int acount = getHint();
				if(acount == 4) {
					playAgain.setEnabled(true);
					guess.setEnabled(false);
					hint.setText("恭喜你！猜对啦！");
				}
			}
		});
		guess.setBounds(62, 160, 93, 23);
		getContentPane().add(guess);

		playAgain = new JButton("\u518D\u73A9\u513F\u4E00\u6B21");
		playAgain.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				initGame();
				guess.setEnabled(true);
			}
		});
		playAgain.setFont(new Font("宋体", Font.PLAIN, 12));
		playAgain.setBounds(216, 160, 113, 23);
		playAgain.setEnabled(false);
		getContentPane().add(playAgain);


		hint = new JLabel("0 A 0 B");
		hint.setHorizontalAlignment(SwingConstants.CENTER);
		hint.setFont(new Font("宋体", Font.BOLD, 18));
		hint.setBounds(102, 130, 172, 30);
		getContentPane().add(hint);

		JTextPane txtpnAb = new JTextPane();
		txtpnAb.setEditable(false);
		txtpnAb.setForeground(Color.BLACK);
		txtpnAb.setBackground(Color.LIGHT_GRAY);
		txtpnAb.setText("A\u4EE3\u8868\u6570\u5B57\u5B58\u5728\u4E14\u4F4D\u7F6E\u6B63\u786E\u7684\u4E2A\u6570\uFF01B\u4EE3\u8868\u6570\u5B57\u5B58\u5728\u4F46\u4F4D\u7F6E\u4E0D\u5BF9\u7684\u4E2A\u6570\uFF01\u5982\uFF1A1A1B\uFF0C\u8868\u793A\u4F60\u731C\u6D4B\u7684\u6570\u5B57\u4E2D\u67092\u4E2A\u5B58\u5728\uFF0C1\u4E2A\u4F4D\u7F6E\u6B63\u786E\uFF0C\u8FD8\u67091\u4E2A\u4F4D\u7F6E\u4E0D\u6B63\u786E\uFF01");
		txtpnAb.setBounds(20, 193, 342, 58);
		getContentPane().add(txtpnAb);

		generateAnswer();
		setVisible(true);
	}
	
	public static void main(String[] args) {
		Main game = new Main();
		game.run();
	}
}